﻿using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Tool;
using TP5Like.DBModel;
using TP5Like.DBModelValidate;

namespace TP5Like.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AddressController : ApiController
    {
        private DataDBContext _context;
        TP5Like.BLL.Address ad = null;
        TP5Like.BLL.MyAddress Myad = null;
        public AddressController(DataDBContext context)
        {
            _context = context;
        }

        // GET: api/Address
        [HttpGet]
        public object Get()
        {
            //ad = new TP5Like.BLL.Address(_context);
            //var data = ad.Get();
            //if (data.Equals(false))
            //{
            //    return Tool.Common.ResultArray(data);
            //}
            //return Tool.Common.ResultArray(data);

            Myad = new TP5Like.BLL.MyAddress(_context);
            var data = Myad.Get();
            return data;

        }

        //根据现有工具库版本，此为最大限度的优化
        // GET: api/Address/5
        [HttpGet("{id}", Name = "Get")]
        public object Get(int id)
        {
           

            ad = new TP5Like.BLL.Address(_context);
            var data = ad.GetId(id);
            if (data.Equals(false))
            {
                return Tool.Common.ResultArray(data);
            }
            return Tool.Common.ResultArray(data);
        }

        // POST: api/Address
        [HttpPost]
        public object Post(Address value)
        {
            //AddressValidate addressValidate = new AddressValidate();
            //var address1 = addressValidate.getProperties<Address>(value);

            //模拟php的验证成功
            ad = new TP5Like.BLL.Address(_context);
            var data = ad.post(value);
            if (data.Equals(false))
            {
                return Tool.Common.ResultArray(data);
            }
            return Tool.Common.ResultArray(data);
           
        }

        // PUT: api/Address/5
        [HttpPut("{id}")]
        public object Put(int id, Address value)
        {
            _context.Addresses.Update(value);
            int aa = _context.SaveChanges();

            if (aa == 1)
            {
                S_Result.msg = "成功";
                return Tool.Common.ResultArray();
            }
            else
            {
                S_Result.error = "失败";
                return Tool.Common.ResultArray();
            }
        }

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public object Delete(int id)
        {
            var list = _context.Addresses.Where(x => x.Id == id).FirstOrDefault();
            if (list == null)
            {
                return Tool.Common.ResultArray("失败");
            }

            _context.Addresses.Remove(list);
            int aa = _context.SaveChanges();

            if (aa == 1)
            {
                S_Result.msg = "成功";
                return Tool.Common.ResultArray();
            }
            else
            {
                S_Result.error = "失败";
                return Tool.Common.ResultArray();
            }
        }
    }
}
